package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Pill;
import com.itheima.service.PillService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/pill")
public class PillController {
    @Reference
    private PillService pillService;

    //文件上传,实现预约设置数据批量导入
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            ArrayList<Pill> data = new ArrayList<>();
            for (String[] strings : list) {
                String code = strings[0];
                String name = strings[1];
                String price = strings[2];
                String unit = strings[3];
                String effect = strings[4];
                String description = strings[5];
                Pill pill = new Pill(null, code, name, price, unit, effect, description);
                data.add(pill);
            }
            //通过dubbo远程调用服务实现数据批量导入到数据库
            pillService.add(data);
            return new Result(true, MessageConstant.IMPORT_LIB_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            //文件解析失败
            return new Result(false, MessageConstant.IMPORT_LIB_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = pillService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Pill pill) {
        try {
            pillService.save(pill);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_PILL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_PILL_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            pillService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PILL_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_PILL_SUCCESS);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Pill pill) {
        try {
            pillService.edit(pill);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_PILL_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_PILL_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Pill pill = pillService.findById(id);
            return new Result(true, MessageConstant.QUERY_SUCCESS, pill);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_FAIL);
        }
    }
}
